//
//  TSSTInfoWindow.m
//  SimpleComic
//
//  Created by Alexander Rauchfuss on 7/15/07.
//  Copyright 2007 Dancing Tortoise Software. All rights reserved.
//

#import "TSSTInfoWindow.h"
#import "CTGradient.h"

@implementation TSSTInfoWindow


- (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag
{
    self = [super initWithContentRect: contentRect styleMask: NSBorderlessWindowMask backing: bufferingType defer: flag];
    if(self)
    {
//        [self setAlphaValue: 0.9];
        [self setOpaque: NO];
        [self setIgnoresMouseEvents: YES];
    }
    return self;
}



- (void)caretAtPoint:(NSPoint)point size:(NSSize)size withLimitLeft:(float)left right:(float)right
{
    float limitWidth = right - left;
    float relativePosition = (point.x - left) / limitWidth;
    NSRect frame = [self frame];
    float offset = NSWidth(frame) * relativePosition;
    [[self contentView] setCaretPosition: offset];
    [self setFrame: NSMakeRect( point.x - offset, point.y, size.width, size.height) display: NO];
    [self display];
    [self invalidateShadow];
}



- (void)centerAtPoint:(NSPoint)center
{
    NSRect frame = [self frame];
    [self setFrameOrigin: NSMakePoint(center.x - NSWidth(frame) / 2, center.y - NSHeight(frame) / 2)];
    [self display];
    [self invalidateShadow];
}




@end



@implementation TSSTInfoView



- (void)setCaretPosition:(float)position
{
    caretPosition = position;
}



- (void)drawRect:(NSRect)aRect
{
    NSRect bounds = [self bounds];
    [[NSColor clearColor] set];
    NSRectFill(bounds);
    
    NSBezierPath * outline = [NSBezierPath bezierPath];
    [outline moveToPoint: NSMakePoint(caretPosition + 5, 5)];
    [outline lineToPoint: NSMakePoint(caretPosition, 0)];
    [outline lineToPoint: NSMakePoint(caretPosition - 5, 5)];
    [outline appendBezierPathWithArcFromPoint: NSMakePoint(0 , 5) 
                                      toPoint: NSMakePoint(0 , NSMidY(bounds)) 
                                       radius: 5];
    [outline appendBezierPathWithArcFromPoint: NSMakePoint(0 , NSMaxY(bounds)) 
                                      toPoint: NSMakePoint(NSMidX(bounds), NSMaxY(bounds)) 
                                       radius: 5];
    [outline appendBezierPathWithArcFromPoint: NSMakePoint(NSMaxX(bounds), NSMaxY(bounds)) 
                                      toPoint: NSMakePoint(NSMaxX(bounds), NSMidY(bounds)) 
                                       radius: 5];
    [outline appendBezierPathWithArcFromPoint: NSMakePoint(NSMaxX(bounds), 5) 
                                      toPoint: NSMakePoint(caretPosition + 5, 5)
                                       radius: 5];
    [outline closePath];
//    [outline setWindingRule: NSEvenOddWindingRule];
    [[NSColor colorWithCalibratedWhite: 0.99 alpha: 0.9] set];
    
    [outline fill];
    [[NSColor colorWithCalibratedWhite: 0.1 alpha: 1] set];
    [outline stroke];
}


@end


@implementation TSSTTransparentContent

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self)
    {
    }
    return self;
}

- (void)drawRect:(NSRect)rect
{
    [[NSColor clearColor] set];
    NSRectFill( rect);
}

@end


@implementation TSSTCircularImageView


- (void)drawRect:(NSRect)rect
{
    if([self image])
    {
        CTGradient * loupeGradient = [CTGradient gradientWithBeginningColor: [NSColor colorWithCalibratedWhite: 0.3 alpha: 1]
                                                                endingColor: [NSColor colorWithCalibratedWhite: 0.6 alpha: 1]];
        NSBezierPath * circle = [NSBezierPath bezierPathWithOvalInRect: [self bounds]];
        [circle addClip];
        [loupeGradient torusFillRect: rect innerRadius: NSWidth(rect) / 2 - 10];
        [[NSColor colorWithCalibratedWhite: 0.2 alpha: 1] set];
        [circle setLineWidth: 2.0];
        [circle stroke];
        circle = [NSBezierPath bezierPathWithOvalInRect: NSInsetRect([self bounds],10,10)];
        [[NSColor whiteColor] set];
        [circle fill];
        [[NSColor colorWithCalibratedWhite: 0.6 alpha: 1] set];
        [circle setLineWidth: 1.0];
        [circle stroke];
        circle = [NSBezierPath bezierPathWithOvalInRect: NSInsetRect([self bounds],11,11)];
        [circle addClip];
    }
    [super drawRect: rect];
}



@end

